package com.yue.springDemo.business.controller;

import com.yue.springDemo.business.bean.ResponseResult;
import com.yue.springDemo.business.bean.User;
import com.yue.springDemo.business.service.HelloService;
import io.swagger.annotations.*;
import jdk.internal.org.objectweb.asm.commons.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hello")
@Validated
@Api(value = "测试接口", tags = {"测试接口"})
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/")
    public String index() {
        return "hello";
    }

    @ApiOperation(value = "获取所有用户1", notes = "获取表中所有数据")
    @ApiImplicitParam(name = "name", value = "姓名", dataType = "String")
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult hello(@PathVariable String name) {
        Map<String, Object> map = new HashMap<>();
        List<User> users = helloService.findAll();
        map.put("name", name);
        map.put("users", users);
        return ResponseResult.success("获取成功", map);
    }

    @ApiOperation(value = "获取所有用户2", notes = "获取表中所有数据")
    @RequestMapping(value = "/find/all", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult findAll(@Validated User user) {
        Map<String, Object> map = new HashMap<>();
        List<User> users = helloService.findAll();
        map.put("name", user.getName());
        map.put("users", users);
        return ResponseResult.success("获取成功", map);
    }

    @ApiOperation(value = "获取所有用户3", notes = "获取表中所有数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄", required = true, paramType = "query", dataType = "int")
    })
    @GetMapping("/find/allByUrl")
    @ResponseBody
    public ResponseResult findAll(@NotBlank(message = "名称不能为空") String name, @NotNull(message = "年龄不能为空") Integer age) {
        Map<String, Object> map = new HashMap<>();
        List<User> users = helloService.findAll();
        map.put("name", name);
        map.put("age", age);
        map.put("users", users);
        return ResponseResult.success("获取成功", map);
    }

    @ApiIgnore
    @RequestMapping("/detail/{id}")
    @ResponseBody
    public ResponseResult detail(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        User user = helloService.findById(id);
        map.put("id", id);
        map.put("user", user);
        return ResponseResult.success("获取成功", map);
    }

    @ApiIgnore
    @PostMapping("/save")
    @ResponseBody
    public ResponseResult save() {
        helloService.saveUser();
        return ResponseResult.success("保存成功");
    }

}
